package com.test.map.mymap;

public class MyMap<K, V> {
    private Node<K, V>[] nodes;

    private int size;

    public void put(K key, V value){
        if(nodes == null){
            nodes = new Node[10];
        }
        int index = indexOfKey(key);
        if(index != -1){
            nodes[index].value = value;
        }else{
            nodes[size] = new Node<>(key, value);
            size++;
        }
    }

    private int indexOfKey(K key) {
        for (int i=0; i<size; i++){
            if(key.equals(this.nodes[i].key)){
                return i;
            }
        }
        return -1;
    }

    public V get(K key){
        int index = indexOfKey(key);
        if(index != -1){
            return this.nodes[index].value;
        }
        return null;
    }

    public int size(){
        return size;
    }


    private static class Node<K, V>{
        K key;
        V value;

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }

    public static void main(String[] args) {
        MyMap map = new MyMap();
        map.put("112","aaa");
        map.put("223","bbb");
        System.out.println(map.get("112"));
        System.out.println(map.size());
    }
}